package com.todo.pro.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.todo.pro.entity.SystemIcon;
import com.todo.pro.handle.EE;
import com.todo.pro.jwt.JwtUtil;
import com.todo.pro.res.JsonResult;
import com.todo.pro.res.model.SystemIconResult;
import com.todo.pro.service.SystemIconService;
import com.todo.pro.util.MapUtil;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * @author SiZhongxia
 * @since 2017-12-27
 */
@Controller
@RequestMapping("icon")
public class IconController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(IconController.class);

	@Autowired
	private SystemIconService systemIconService;

	@RequiresAuthentication
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("modules/icon/list");
		mav.addObject("csrftoken",
				JwtUtil.createToken(MapUtil.init().putVal("path", "gq-icon-list"), 2L * 60 * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "data")
	public JsonResult<List<SystemIconResult>> data(HttpServletRequest request) {
		Wrapper<SystemIcon> iconWrapper = new EntityWrapper<>();

		String keyword = request.getParameter("keyword");
		if (StringUtils.isNotBlank(keyword)) {
			iconWrapper.like("name", keyword).or().like("icon", keyword);
		}

		iconWrapper.orderBy("icon", true);
		List<SystemIcon> icons = systemIconService.selectList(iconWrapper);

		List<SystemIconResult> iconrs = new ArrayList<>();
		int count = icons.size();
		if (count > 0) {
			for (SystemIcon icon : icons) {
				SystemIconResult iconr = new SystemIconResult();
				BeanUtils.copyProperties(icon, iconr);
				iconr.setId(icon.getId().toString());
				iconrs.add(iconr);
			}
		}
		return new JsonResult<List<SystemIconResult>>(count, iconrs);
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "updateField")
	public JsonResult<Object> updateField(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-icon-list".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}

		String field = request.getParameter("field");
		if (StringUtils.isBlank(field)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String value = request.getParameter("value");
		if (StringUtils.isBlank(value)) {
			value = "";
		}

		SystemIcon systemIcon = systemIconService.selectById(id);
		if (systemIcon == null) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}
		if ("name".equals(field)) {
			systemIcon.setName(value);
		} else if ("icon".equals(field)) {
			systemIcon.setIcon(value);
		} else {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		systemIcon.setUpdateTime(DateUtil.date());
		if (!systemIconService.updateById(systemIcon)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@RequiresAuthentication
	@RequestMapping(value = "add")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("modules/icon/add");
		mav.addObject("csrftoken", JwtUtil.createToken(MapUtil.init().putVal("path", "gq-icon-add"), 30L * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "save")
	public JsonResult<Object> save(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-icon-add".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String icon = request.getParameter("icon");
		if (StringUtils.isBlank(icon)) {
			icon = "layui-icon-set";
		}

		SystemIcon systemIcon = new SystemIcon();
		systemIcon.setName(name);
		systemIcon.setIcon(icon);
		systemIcon.setCreateTime(DateUtil.date());
		systemIcon.setUpdateTime(DateUtil.date());
		if (!systemIconService.insert(systemIcon)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "delete")
	public JsonResult<Object> delete(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-icon-list".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String[] ids = request.getParameterValues("ids[]");
		if (ids == null || ids.length == 0) {
			return new JsonResult<Object>(false, EE.SELECT_LEAST_ONE_RECORD.i());
		}

		Set<Long> deleteIconIds = new HashSet<>();
		for (String iconId : ids) {
			deleteIconIds.add(new Long(iconId));
		}

		if (!systemIconService.deleteBatchIds(deleteIconIds)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

}
